#!/usr/bin/env python
#
#   Copyright (C) 2006 by Jonathan Saggau                                 
#   saggau@gmail.com                                                   
#                                                                         
#   This program is free software; you can redistribute it and/or modify  
#   it under the terms of the GNU General Public License as published by  
#   the Free Software Foundation; either version 2 of the License, or     
#   (at your option) any later version.                                   
#                                                                         
#   This program is distributed in the hope that it will be useful,       
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#   GNU General Public License for more details.                          
#                                                                         
#   You should have received a copy of the GNU General Public License     
#   along with this program; if not, write to the                         
#   Free Software Foundation, Inc.,                                       
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  
#

import time, os

from scosc import controller
from supyrconductor import synth, node, process, buffer, server

class SynthdefTest(object):
    _ampScaler = 100
    _numInputs = 4
    _sensitivity = 0.0001
    _testInput = 1
    _soundFile = os.path.expanduser("~/kryptoniteSounds/PerformanceSounds/fugueMono/WhisperD4FlangeRaw.wav.short.wav")
    _bufferID = 1
    _testTime = 10
    _duration = .4
    
    def setUp(self):
        self._synthArgs = {"envCurve":-7, 
                           "attackDuration":0.05, 
                           "bufnum":self._bufferID, 
                           "outBus":0, 
                           "triggerID":1000, 
                           "thresh":self._sensitivity,
                           "in":self._testInput, 
                           "duration":self._duration, 
                           "amp":self._ampScaler, 
                           "bufferPosition":200,
                           "pan":0}
        self.controller = controller.Controller(('127.0.0.1', 57110),
                                                verbose=True, spew=True)
        #self.controller.listSendMsg(server.dumpOSC(1))
        self.controller.listSendMsg(server.notify(1))
        self.controller.receive("/done")
        self.buffer = buffer.Buffer(self.controller, ID=self._bufferID, 
                                    filePath=self._soundFile)
        self.buffer.allocRead()
        print "Buffer info = " + str(self.buffer.query())
        time.sleep(3)
        #self.instance = process.start_local(verbose = True)
        
    def test_inputs(self):
        print "TESTING INPUTS"
        time.sleep(1)
        nodes  =  [each+1000 for each in range(self._numInputs)]
        for i in nodes:
            self.controller.listSendMsg(synth.new("JASTrigger", i, 
                            synth.HEAD, 0, {"in":i-1000, 
                                            "thresh":self._sensitivity, 
                                            "triggerID":i}))
        time.sleep(self._testTime)
        self.controller.listSendMsg(node.free(nodes))
        
    def test_input(self):
        print "TESTING INPUT"
        time.sleep(1)
        args = {"in":self._testInput, 
                "thresh":self._sensitivity, 
                "triggerID":self._testInput}
        self.controller.listSendMsg(synth.new("JASTrigger", 1000, 
                        synth.HEAD, 0, args))
        time.sleep(self._testTime)
        self.controller.listSendMsg(node.free(1000))
        
    def _testSynth(self, name):
        self.controller.listSendMsg(synth.new(name, 1000, synth.HEAD, 0, 
                                              self._synthArgs))
        time.sleep(self._testTime)
        self.controller.listSendMsg(node.free(1000))
        
    def test_oldSynth(self):
        print "TESTING OLD SYNTH"
        time.sleep(1)
        self._testSynth("JASTriggerBOlap16")
        
    def test_newSynth(self):
        print "TESTING NEW SYNTH"
        time.sleep(1)
        self._testSynth("JASTrigMono16statperc")
        
    def test_tgrainsSynth(self):
        print "TESTING TGRAINS SYNTH"
        time.sleep(1)
        args = {"out":0, "bufnum":self._bufferID, "in":self._testInput, 
                "thresh":self._sensitivity, "amp":self._ampScaler, 
                "duration":0.2, "triggerID":1000, "pan":0.5, 
                "centerPosition":0.1, "interp":1}
        self.controller.listSendMsg(synth.new("JASTriggerBufferGranular", 
                                              1000, synth.HEAD, 0, args))
        time.sleep(self._testTime)
        self.controller.listSendMsg(node.free(1000))

    def tearDown(self):
        self.buffer.close()
        #self.instance.kill()
    
    def main(self):
        self.setUp()
        self.test_input()
        self.test_inputs()
        self.test_oldSynth()
        self.test_newSynth()
        self.test_tgrainsSynth()
        self.tearDown()

if __name__ == '__main__':
    x = SynthdefTest()
    x.main()
